package com.kewojiaoyu.splitscreendemo;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

public class MainActivity extends AppCompatActivity {

    View mLine;
    FrameLayout frameLayout0;
    FrameLayout frameLayout1;
    /**
     * 上一次x坐标
     */
    private float beforeY;
    private float mtop;
    private float mbottom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        frameLayout0 = (FrameLayout) findViewById(R.id.frag0);
        frameLayout1 = (FrameLayout) findViewById(R.id.frag1);
        mLine = findViewById(R.id.addLine);
        getSupportFragmentManager().beginTransaction().add(R.id.frag0, new MineFragment(0)).commit();
        getSupportFragmentManager().beginTransaction().add(R.id.frag1, new MineFragment(1)).commit();

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (!(event.getY() > mLine.getTop() && event.getY() < mLine.getBottom())) {
                    return super.onTouchEvent(event);
                }
                beforeY = event.getY();
                mtop = event.getY() - mLine.getTop();
                mbottom =mLine.getBottom()- event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                frameLayout0.getLayoutParams().height =(int)(event.getY()-getStatusBarHeight(this)-mtop+0.5f);
                frameLayout1.getLayoutParams().height = (int)(GetScreenHeight(this) - event.getY()-mbottom+0.5f);
                frameLayout0.requestLayout();
                frameLayout1.requestLayout();


                break;
            case MotionEvent.ACTION_UP:
                beforeY = event.getY();
                break;

        }
        return true;

    }

    /**
     * 可见屏幕高度
     **/
    public int getAppHeight(Activity paramActivity) {
        Rect localRect = new Rect();
        paramActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(localRect);
        return localRect.height();
    }
    public static int GetScreenHeight(Context context) {
        DisplayMetrics mDisplayMetrics = context.getResources()
                .getDisplayMetrics();
        return mDisplayMetrics.heightPixels;
    }
    /**
     * statusBar高度
     **/
    public static int getStatusBarHeight(Activity paramActivity) {
        Rect localRect = new Rect();
        paramActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(localRect);
        return localRect.top;
    }

}
